package com.chat.api.web.vo;

import com.chat.api.common.exception.ApiException;
import com.chat.api.utils.util.ContextUtil;
import com.chat.api.utils.util.DateUtil;
import com.chat.api.utils.util.ParamUtil;
import com.chat.api.web.po.ChatBean;
import com.chat.api.web.service.UserService;
import lombok.Data;

import java.io.Serializable;

@Data
public class ChatEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    private String id;
    private String userId;
    private String message;
    private String time;
    private String isRead;
    private String receiver;

    private String senderName;
    private String receiverName;

    public void setEntity(ChatBean bean){
        this.setId(String.valueOf(bean.getId()));
        this.setUserId(String.valueOf(bean.getUserId()));
        this.setMessage(bean.getMessage());
        this.setTime(DateUtil.dateToStr(bean.getCreateDate(),12));
        this.setIsRead(String.valueOf(bean.getIsRead()));
        this.setSenderName(getName(bean.getUserId()));
        this.setReceiverName(getName(bean.getReceiver()));
        this.setReceiver(String.valueOf(bean.getReceiver()));
    }

    public void setBeanValue(ChatBean bean){
        bean.setId(ParamUtil.getLongParameter(id,-1L));
        bean.setUserId(ParamUtil.getLongParameter(userId,-1L));
        bean.setMessage(message);
        bean.setIsRead(Integer.parseInt(isRead));
        bean.setReceiver(ParamUtil.getLongParameter(receiver,-1L));
    }

    public String getName(Long userId){
        UserService userService = ContextUtil.getBean(UserService.class);
        if (userId == null){
            throw new ApiException("userId不能为空！！！");
        }
        String name = userService.getNameById(userId);
        return name;
    }
}
